package com.youdian.activity;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.youdian.R;
import com.youdian.fragment.ExpressFragment;
import com.youdian.fragment.MainFragment;
import com.youdian.fragment.MineFragment;
import com.youdian.fragment.PagerAdapter;
import com.youdian.widget.navbar.CircularRevealButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    @BindViews({R.id.crbtn_main,R.id.crbtn_express,R.id.crbtn_mine})
    List<CircularRevealButton> crbs;

    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    private List<Fragment> fragments;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initViewPagers();
    }


    public void initViewPagers(){
        fragments = new ArrayList<>();
        Fragment main = new MainFragment();
        Fragment express = new ExpressFragment();
        Fragment mine = new MineFragment();
        fragments.add(main);
        fragments.add(express);
        fragments.add(mine);
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(),fragments);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(this);
    }


    @OnClick({R.id.crbtn_main,R.id.crbtn_express,R.id.crbtn_mine})
    public void onClick(CircularRevealButton button){
        onSelect(button);
    }


    public void onSelect(CircularRevealButton selectBtn){

        int length = crbs.size();

        for(int i=0;i<length;i++) {
            CircularRevealButton crb = crbs.get(i);
            if(crb.getId() == selectBtn.getId()){
                if(!crb.isSelected()){
                    crb.setonSelected(true);
                    mViewPager.setCurrentItem(i);
                }
            }else{
                crb.setonSelected(false);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        onSelect(crbs.get(position));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
